import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import enigma.console.Console;
import enigma.core.Enigma;

public class ListenerClient 
{
	private Console console = Enigma.getConsole("Client Window");
	private DisplayStream display = new DisplayStream(console);
	private Listener listener;
	private int delay;
	private int sampleRate;
	public void setDelay(int delay)
	{
		this.delay = delay;
	}
	public void setSampleRate(
			int sampleRate)
	{
		this.sampleRate = sampleRate;
	}
	protected void println(Object str){
		console.getOutputStream().println(str);
	}
	public ListenerClient() throws Exception
	{
		//byte[] receiveData = new byte[70];
		String[] requests = {"1","2"};
		
		final DatagramSocket clientSocket = new DatagramSocket();
		InetAddress serverIPAddress = InetAddress.getByName("localhost");
	
		BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); 
		
		println("To start transmission type: 101 START");
		println("To stop transmission type: 102 STOP");
		
		String userMessage = userInput.readLine().trim();

		if(userMessage.equals(requests[0])){
			RadioPacket p = new RadioPacket(100);
			p.setPacketType("STA".getBytes());
			byte[] sendData = p.getBytes();
			//System.arraycopy(requests[0].getBytes(),0,sendData,0,requests[0].getBytes().length);
			DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,serverIPAddress, 8000);
			clientSocket.send(sendPacket);
			System.out.println(p.getPacketType());
			listener = new Listener(this,clientSocket, display);
			listener.start();
		}
		
		println("To stop transmission type: 102 STOP");
		while(true)
		{
			userInput = new BufferedReader(new InputStreamReader(System.in)); 
			
			
			
			userMessage = userInput.readLine().trim();
			if(userMessage.equals(requests[1])){
				RadioPacket p = new RadioPacket(100);
				p.setPacketType("STO".getBytes());
				byte[] sendData = p.getBytes();
				//System.arraycopy(requests[1].getBytes(),0,sendData,0,requests[1].getBytes().length);
				DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,serverIPAddress, 8000);
				clientSocket.send(sendPacket);
				listener.running = false;
				//listener = null;
				break;
			}
			
		}
	}
	
	private static class Listener extends Thread{
		public boolean running = true;
		private DatagramSocket clientSocket;
		private DisplayStream stream;
		private ListenerClient client;
		public Listener(ListenerClient client, DatagramSocket clientSocket, DisplayStream stream)
		{
			this.client = client;
			this.stream = stream;
			this.clientSocket = clientSocket;
		}
		public void run()
		{
			try{
				while(running){
					byte[] receiveData = new byte[100];
					DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
					clientSocket.receive(receivePacket);
					receiveData = receivePacket.getData();
					RadioPacket p = new RadioPacket(receiveData);
					if(p.getPacketType().equals("CON")){
						client.setSampleRate(p.getDataSampleRate());
						client.setDelay(p.getDataDelay());
						client.display.setDelay(client.delay);
						client.display.setInterval(client.sampleRate);
						client.display.start();
					} else if(p.getPacketType().equals("DAT")){
						client.display.queueData(p);
						//System.out.println(p.getData());
					}
					//stream.getOutputStream().println(new String(receiveData));
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	};
	
	public String getSample(){
		return null;
	}
	public static void main(String[] args) throws Exception{
		ListenerClient client = new ListenerClient();
		
	}
}